package com.cyt.web.controller.sys;

import com.cyt.base.enums.BusinessType;
import com.cyt.base.enums.OperatorType;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.sys.aspect.BaseLog;
import com.cyt.sys.model.po.SysMenu;
import com.cyt.sys.model.vo.SysMenuVo;
import com.cyt.sys.service.SysMenuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 基础部分-菜单控制层
 *
 * @author rong yuan
 * @date 2019-04-23 11:20
 */
@RestController
public class SysMenuController extends SysBaseController {

    @Resource
    private SysMenuService sysMenuService;

    /**
     * 菜单模块统一跳转处理
     *
     * @param model 对象
     * @return Model
     * @author rong yuan
     * @date 2019-8-13 09:32
     */
    @RequestMapping("/sys/menu/*")
    public Model goPage(Model model) {
        return model;
    }

    /**
     * 获取菜单集合
     *
     * @param sysMenuVo 参数传入对象
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-8-13 09:32
     */
    @BaseLog(module = "系统模块", classz = "菜单管理", methods = "菜单List", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/sysMenu/list", method = RequestMethod.POST)
    public ReturnResult list(SysMenuVo sysMenuVo) {
        return new ReturnResult(sysMenuService.list(sysMenuVo));
    }

    /**
     * 跳转添加或编辑页面
     *
     * @param sysMenuVo 参数传入对象
     * @return ModelAndView
     * @author rong yuan
     * @date 2019-8-13 09:32
     */
    @RequestMapping(value = "/sysMenu/getEdit", method = RequestMethod.GET)
    public ModelAndView getEdit(SysMenuVo sysMenuVo) {
        ModelAndView view = new ModelAndView("/sys/menu/menuEdit");
        SysMenu sysMenu = new SysMenu();
        if (StringUtils.isNotBlank(sysMenuVo.getId())) {
            sysMenu = sysMenuService.selectByPrimaryKey(sysMenuVo.getId());
            if (StringUtils.isNotBlank(sysMenu.getParentId())) {
                SysMenu parentMenu = sysMenuService.selectByPrimaryKey(sysMenu.getParentId());
                sysMenu.setParentName(parentMenu.getMenuName());
            }
        }
        view.addObject(sysMenu);
        return view;
    }

    /**
     * 保存/更新菜单信息
     *
     * @param sysMenuVo 参数传入对象
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-8-13 09:32
     */
    @BaseLog(module = "系统模块", classz = "菜单管理", methods = "保存/更新菜单", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/sysMenu/addOrUpdate", method = RequestMethod.POST)
    public ReturnResult addOrUpdate(HttpServletRequest request, SysMenuVo sysMenuVo) {
        return sysMenuService.addOrUpdate(sysMenuVo, acceptCurrentUser(request));

    }

    /**
     * 逻辑删除菜单信息
     *
     * @param sysMenuVo 参数传入对象
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-8-13 09:32
     */
    @BaseLog(module = "系统模块", classz = "菜单管理", methods = "菜单删除", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/sysMenu/remove", method = RequestMethod.GET)
    public ReturnResult remove(HttpServletRequest request, SysMenuVo sysMenuVo) {
        return sysMenuService.remove(sysMenuVo, acceptCurrentUser(request));
    }

    /**
     * 获取菜单图树
     *
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-8-13 09:32
     */
    @BaseLog(module = "系统模块", classz = "菜单管理", methods = "菜单树", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/sysMenu/dTreeList", method = RequestMethod.POST)
    public ReturnResult dTreeList(SysMenuVo sysMenuVo) {
        return sysMenuService.dTreeList(sysMenuVo);
    }
}